package config

import (
	"log"
	"os"
	"path/filepath"
	"strconv"
	"strings"
	"time"
)

const (
	ListenAddr           = "0.0.0.0:9091"
	ContainerLogTailLine = 2000
	//打印mysql debug日志开关
	LogMode = false
	//连接池配置
	MaxIdleConns = 10               //最大空闲连接
	MaxOpenConns = 100              //最大连接数
	MaxLifeTime  = 30 * time.Second //最大生存时间

)

type Config struct {
	DbUser     string
	DbPassword string
	DbHost     string
	DbPort     int
	DbName     string

	//file url
	//SrcFile string
	DstFile string
	//Kubeconfig           = "D:\\config"
	AdminUser string
	AdminPwd  string
	//KubeconfigDir string
	//YamlsDir string
	//gitlab
	Gitlan_url   string
	Gitlab_token string
	//Jenkins登入信息
	Jenkins_url           string
	Jenkins_user          string
	Jenkins_pass          string
	Jenkins_Notifications string
	//Harbor登入信息
	Harbor_url  string
	Harbor_user string
	Harbor_pass string
}

type JenkinsConfigTest struct{}

func (j *JenkinsConfigTest) ConfigTest() (*Config, error) {

	// Retrieve the configuration file path from the environment variable
	configFilePath := os.Getenv("CONFIGFILEPATH")

	print("当前配置变量", configFilePath)

	var content string
	if configFilePath == "" {
		// Use the = operator to assign values to already declared variables
		// Convert the byte slice to a string using string()
		contentBytes, err := os.ReadFile(`D:\configtest.txt`)
		if err != nil {
			log.Fatalf("配置文件地址为空切换为开发地址 Failed to read file: %v", err)
		}
		content = string(contentBytes) // Convert byte slice to string
	} else {
		contentBytes, err := os.ReadFile(configFilePath)
		if err != nil {
			log.Fatalf("Failed to read file: %v", err)
		}
		content = string(contentBytes) // Convert byte slice to string
	}

	configContent := string(content)
	lines := strings.Split(configContent, "\n")

	config := &Config{}

	for _, line := range lines {
		parts := strings.SplitN(line, "=", 2)
		if len(parts) == 2 {
			key := strings.TrimSpace(parts[0])
			value := strings.TrimSpace(parts[1])

			switch key {
			case "DbUser":
				config.DbUser = value
			case "DbPassword":
				config.DbPassword = value
			case "DbHost":
				config.DbHost = value
			case "DbPort":
				port, err := strconv.Atoi(value)
				if err != nil {
					log.Fatalf("Failed to parse DbPort: %v", err)
				}
				config.DbPort = port
			case "DbName":
				config.DbName = value
			//case "SrcFile":
			//	//file url
			//	config.SrcFile = value
			case "DstFile":
				config.DstFile = value
				//Kubeconfig           = "D:\\config"
			case "AdminUser":
				config.AdminUser = value
			case "AdminPwd":
				config.AdminPwd = value
			//case "KubeconfigDir":
			//	config.KubeconfigDir = value
			//case "YamlsDir":
			//	config.YamlsDir = value
			//gitlab
			case "Gitlan_url":
				config.Gitlan_url = value
			case "Gitlab_token":
				config.Gitlab_token = value
				//Jenkins登入信息
			case "Jenkins_url":
				config.Jenkins_url = value
			case "Jenkins_user":
				config.Jenkins_user = value
			case "Jenkins_pass":
				config.Jenkins_pass = value
			case "Jenkins_Notifications":
				config.Jenkins_Notifications = value
			case "Harbor_url":
				config.Harbor_url = value
			case "Harbor_user":
				config.Harbor_user = value
			case "Harbor_pass":
				config.Harbor_pass = value
			}
		}
	}

	return config, nil
}

// 文件复制
var ProjectRoot, _ = os.Getwd()

var FilePath = filepath.Join("config", "servermain.py")

var YamlsDir = filepath.Join("config")

// 2. 模板文件路径（对应你的真实路径：项目根目录/k8s-platform/config/automation_install.sh.tpl）
var AutomationTplPath = filepath.Join(ProjectRoot, "config", "automation_install.sh.tpl")

// 3. 生成的脚本路径（和模板同目录）
var AutomationScriptPath = filepath.Join(ProjectRoot, "config", "automation_install.sh")

// keepalived 路径
var KeepalivedTplPath = filepath.Join(ProjectRoot, "config", "keepalived.yaml.tpl")

var KeepalivedScriptPath = filepath.Join(ProjectRoot, "config", "keepalived.yaml")

var K8s_install_logs = filepath.Join(ProjectRoot, "logs")
